package com.czk.yygh.common.utils;

import com.czk.yygh.common.helper.JwtHelper;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/24 22:14
 */
public class AuthContextHolder {
    /**
     * 获取当前用户的id
     *
     * @param request
     * @return
     */
    public static Long getUserId(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)) {
            Long userId = JwtHelper.getUserId(token);
            return userId;
        }
        return null;
    }

    /**
     * 获取当前用户的名称
     *
     * @param request
     * @return
     */
    public static String getUsername(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (!StringUtils.isEmpty(token)) {
            String userName = JwtHelper.getUserName(token);
            return userName;
        }
        return null;
    }

}
